<script setup>
import { ref, onMounted } from 'vue'
import myAxios from '../../../plugins/myAxios'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Edit, Delete } from '@element-plus/icons-vue'

// 数据定义
const subjects = ref([]) // 科目列表
const selectedSubject = ref(null) // 当前选中的科目
const essentialsList = ref([]) // 重难点和考试要点列表
const dialogVisible = ref(false) // 对话框显示状态
const formData = ref({
  content: '',
  type: 1, // 1-重难点 2-考试要点
  researchSubject: null
})

// 获取科目列表
const getSubjects = async () => {
  try {
    const res = await myAxios.get('/teacher/researchInfo/subject/list')
    subjects.value = res
  } catch (error) {
    ElMessage.error('获取科目列表失败')
  }
}

// 获取重难点和考试要点列表
const getEssentialsList = async (subjectId) => {
  try {
    const res = await myAxios.get(`/teacher/testEssentials/list?subjectId=${subjectId}`)
    essentialsList.value = res
  } catch (error) {
    ElMessage.error('获取列表失败')
  }
}

// 选择科目
const handleSelectSubject = (subject) => {
  selectedSubject.value = subject
  getEssentialsList(subject.id)
}

// 编辑信息
const handleEdit = (row) => {
  formData.value = {
    id: row.id,
    content: row.content,
    type: row.type,
    researchSubject: row.researchSubject
  }
  dialogVisible.value = true
}

// 删除信息
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该信息吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      await myAxios.post(`/teacher/testEssentials/delete?id=${row.id}`)
      ElMessage.success('删除成功')
      getEssentialsList(selectedSubject.value.id)
    } catch (error) {
      ElMessage.error('删除失败')
    }
  })
}

// 修改 handleAdd 方法为 handleSubmit，支持新增和编辑
const handleSubmit = async () => {
  if (!formData.value.content) {
    ElMessage.warning('请输入内容')
    return
  }

  // 准备提交的数据
  const submitData = {
    content: formData.value.content,
    type: formData.value.type,
    subjectId: selectedSubject.value.id
  }

  // 如果是编辑，需要添加id
  if (formData.value.id) {
    submitData.id = formData.value.id
  }

  try {
    if (formData.value.id) {
      // 编辑
      await myAxios.post('/teacher/testEssentials/update', submitData)
      ElMessage.success('修改成功')
    } else {
      // 新增
      await myAxios.post('/teacher/testEssentials/add', submitData)
      ElMessage.success('添加成功')
    }
    dialogVisible.value = false
    getEssentialsList(selectedSubject.value.id)
    resetForm()
  } catch (error) {
    ElMessage.error(formData.value.id ? '修改失败' : '添加失败')
  }
}

// 重置表单
const resetForm = () => {
  formData.value = {
    content: '',
    type: 1,
    researchSubject: null
  }
}

// 关闭对话框时重置表单
const handleDialogClose = () => {
  resetForm()
}

onMounted(() => {
  getSubjects()
})
</script>

<template>
  <div class="essentials-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">重难点管理</h1>
      <div class="page-desc">管理各学科的重难点和考试要点</div>
    </div>

    <!-- 科目选择区域 -->
    <div class="subjects-area">
      <el-card class="subject-card"
        v-for="subject in subjects"
        :key="subject.id"
        :class="{ 'selected': selectedSubject?.id === subject.id }"
        @click="handleSelectSubject(subject)"
      >
        <div class="subject-content">
          <span class="subject-name">{{ subject.subjectName }}</span>
        </div>
      </el-card>
    </div>

    <!-- 信息展示区域 -->
    <el-card v-if="selectedSubject" class="content-area">
      <template #header>
        <div class="card-header">
          <span class="title">{{ selectedSubject.subjectName }} - 重难点/考试要点管理</span>
          <el-button type="primary" @click="dialogVisible = true">
            <el-icon><Plus /></el-icon>添加新信息
          </el-button>
        </div>
      </template>

      <el-table
        :data="essentialsList"
        border
        stripe
        highlight-current-row
      >
        <el-table-column prop="content" label="内容">
          <template #default="scope">
            <div class="content-cell">{{ scope.row.content }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="type" label="类型" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.type === 1 ? 'success' : 'warning'">
              {{ scope.row.type === 1 ? '重难点' : '考试要点' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" align="center" />
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="handleEdit(scope.row)">
              <el-icon><Edit /></el-icon>修改
            </el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">
              <el-icon><Delete /></el-icon>删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="formData.id ? '修改信息' : '添加新信息'"
      width="500px"
      @close="handleDialogClose"
    >
      <el-form :model="formData" label-width="80px">
        <el-form-item label="类型">
          <el-radio-group v-model="formData.type">
            <el-radio :label="1">重难点</el-radio>
            <el-radio :label="2">考试要点</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="内容">
          <el-input
            v-model="formData.content"
            type="textarea"
            rows="4"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="less">
.essentials-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 40px);

  .page-header {
    margin-bottom: 30px;
    padding-bottom: 20px;
    border-bottom: 1px solid #e4e7ed;

    .page-title {
      font-size: 28px;
      font-weight: 600;
      color: #303133;
      margin: 0 0 8px 0;
    }

    .page-desc {
      font-size: 14px;
      color: #909399;
    }
  }

  .subjects-area {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 30px;

    .subject-card {
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      }

      &.selected {
        border: 2px solid var(--el-color-primary);
        transform: translateY(-5px);
      }

      .subject-content {
        padding: 20px;
        text-align: center;

        .subject-name {
          font-size: 16px;
          font-weight: bold;
          color: #303133;
        }
      }
    }
  }

  .content-area {
    margin-top: 20px;

    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .title {
        font-size: 18px;
        font-weight: bold;
        color: #303133;
      }
    }

    .content-cell {
      padding: 8px 0;
      line-height: 1.5;
      white-space: pre-wrap;
    }
  }
}

// 对话框样式优化
:deep(.el-dialog) {
  border-radius: 8px;

  .el-dialog__header {
    margin-right: 0;
    padding: 20px;
    border-bottom: 1px solid #dcdfe6;
  }

  .el-dialog__body {
    padding: 30px 20px;
  }

  .el-dialog__footer {
    padding: 20px;
    border-top: 1px solid #dcdfe6;
  }
}

// 表单样式优化
:deep(.el-form-item) {
  margin-bottom: 25px;

  .el-form-item__label {
    font-weight: 500;
  }
}
</style>
